package com.microsoft.mcp.sample.client;

import io.modelcontextprotocol.client.McpClient;
import io.modelcontextprotocol.client.transport.WebFluxSseClientTransport;
import io.modelcontextprotocol.spec.McpClientTransport;
import io.modelcontextprotocol.spec.McpSchema;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.Map;

/**
 * 简单的手搓 mcp client 调用（无模型）
 */
public class SDKClient {
  private final McpClientTransport transport;

  public static void main(String[] args) {
    // sse
    var transport = new WebFluxSseClientTransport(WebClient.builder().baseUrl("http://localhost:8080"));
    new SDKClient(transport).run();
  }

  public SDKClient(McpClientTransport transport) {
    this.transport = transport;
  }

  public void run() {
    var client = McpClient.sync(this.transport).build();
    client.initialize();
    // 这里编写 客户端逻辑

    // 查询 tools
    McpSchema.ListToolsResult toolsList = client.listTools();
    System.out.println("Avaliable Tools=" + toolsList);

    // ping 服务端 验证连接
    client.ping();

    // 调用工具
    McpSchema.CallToolResult resultAdd = client.callTool(new McpSchema.CallToolRequest("add", Map.of("a", 5, "b", 3)));
    System.out.println("Add Result = " + resultAdd);
  }
}
